import { Global } from "./Global";

const {ccclass, property} = cc._decorator;

@ccclass


export default class NewClass extends cc.Component {

    @property(cc.Prefab)
    waterPrefab: cc.Prefab = null;

    @property(cc.Node)
    typeNode: cc.Node =null;

    @property(cc.Node)
    deathNode:cc.Node=null;

    @property(cc.SpriteAtlas)
    spriteAtlas:cc.SpriteAtlas=null;

    isCanDown:boolean;
    
    onLoad () {
        this.isCanDown=true;
        this.node.on(cc.Node.EventType.TOUCH_START,this.onTouchStart,this);
    }

    onTouchStart(event:cc.Event.EventTouch){
        cc.log('touch '+this.isCanDown);
        if(!this.isCanDown){
            return;
        }

        this.isCanDown=false;
        this.scheduleOnce(()=>{
            this.isCanDown=true;
        }, 1);
        let pos=this.node.convertToNodeSpaceAR(event.getLocation());
        this.spawnWater(pos);
        this.randomType();
    }

    //生成水果
    spawnWater(pos:cc.Vec2){
        let water=cc.instantiate(this.waterPrefab);
        this.node.addChild(water);
        
        water.x=pos.x;
        water.y=this.deathNode.y-120;
    }

    //随机类型
    randomType(){
        let random=Math.random()*5|0;
        let sprite=this.typeNode.getComponent(cc.Sprite);
        sprite.spriteFrame=this.spriteAtlas.getSpriteFrames()[random];
        Global.type=random;
    }
}
